<?php

/*********************************************************************************
* Filename: schedule_generation.php
* 
* This PHP Script schedules the regular season games for a given league (retrieved
* from session variable.
*
*
* Author: Caleb Goff
* Date Created: 12/15/2009
**********************************************************************************/

//Start the PHP session is it doesn't exist
if (!isset($_SESSION))
	session_start();
//Include common.php to utilize common functions
include_once 'common.php';
//Retrieve variables from HTTP POST
$team_id      = $_SESSION['team_id'];

$retVal = 0;

// Query the database to check the league password against the one provided
$query = "SELECT LEAGUE_ID FROM TEAMS WHERE TEAM_ID = '$team_id'";
$result = do_query($query, $g_db_name, $g_username);
$row = mysql_fetch_assoc($result);

// Retrieve the League ID
$league_id = $row['LEAGUE_ID'];

$query = "SELECT SCHEDULED FROM LEAGUES WHERE LEAGUE_ID = '$league_id'";
$result = do_query($query, $g_db_name, $g_username);
$row = mysql_fetch_assoc($result);

// Check sheduled status 
$scheduled = $row['SCHEDULED'];

if ($scheduled != 1) {

    echo "League ID: $league_id<br><br>";

    $query = "SELECT TEAM_ID FROM TEAMS WHERE LEAGUE_ID = '$league_id'";
    $result = do_query($query, $g_db_name, $g_username);

    $week = 1;

    while ($row = mysql_fetch_assoc($result)) {
        $teams[] = $row['TEAM_ID'];
        $test = $row['TEAM_ID'];
        echo "$test id<br><br>";
    }

    $even = sizeof($teams) % 2;

    if ($even > 0) {
        $teams[] = 0;
    }

    $cap = sizeof($teams);
    $secCap = $cap - 1;

    //for ($i = 0; $i < sizeof($teams); $i++) {
    //    echo $teams[$i];
    //    echo "<br>";
    //}

    //echo "<br><br>";

    for ($i = 0; $i < 8; $i++) {

        echo "Week '$week'<br>";

        if ($cap > 2) {
            for ($j = 0; $j < sizeof($teams); $j=$j+2) {
                
                $next = ($j + 1);
            
                //echo "J = $j Next = $next<br>";
               
                $home_team = $teams[$j];
                $away_team = $teams[$next];

                echo "Match-up: '$home_team' - '$away_team'<br><br>";
                
                if ($home_team != 0) {
                    if ($away_team != 0) {
                        $insert_query = "INSERT INTO GAMES (TEAM1, TEAM2, WEEK)
                                         VALUES('$home_team','$away_team','$week')";
                        do_query($insert_query, $g_db_name, $g_username);
                    }
                }
            }

            $shift = 0;

            for ($k = 1; $k < $cap; $k++) {
                
                //echo "<br><b>$k</b><br>";
                
                if ($k < $secCap) { 
                    if ($k == 1) {
                        $old = $teams[$k];
                        $shift = $teams[$k+1];
                        $teams[$k+1] = $old;
                    } else {
                        $temp = $teams[$k+1];
                        $teams[$k+1] = $shift;
                        $shift = $temp;
                        //echo "Shift val: $shift";
                    }   
                }
            }
            
            $teams[1] = $shift;

        } else {
            
            // Case 2
            $insert_query = "INSERT INTO GAMES (TEAM1, TEAM2, WEEK)
                             VALUES('$teams[0]','$teams[1]','$week')";
            do_query($insert_query, $g_db_name, $g_username);

        }

        $week++;

    }

    $update_query = "UPDATE LEAGUES SET SCHEDULED = 1 WHERE LEAGUE_ID = '$league_id'";
    do_query($update_query,$g_db_name,$g_username);

} else {

    // Display error code if necessary

}

$path = $g_server . "/league_home.php";
header( "Location: $path" );

?>

